package com.tungse.study.chat;

import org.junit.Test;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author Tungse
 */
public class ClientChat {
    public static void main(String[] args) throws IOException, InterruptedException {
        Thread ta = new Thread(ClientChat::sendMsg, "A");
        Thread tb = new Thread(ClientChat::sendMsg, "B");
        ta.start();
        tb.start();
        ta.join();
        tb.join();

    }

    public static void sendMsg () {
        try {
            Socket socket = new Socket("127.0.0.1", 9999);
            for(int i=0; i<10; i++) {
                String msg = Thread.currentThread().getName() + "hello" + i;
                socket.getOutputStream().write(msg.getBytes());
            }
            socket.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

//    @Test
//    public void test() throws IOException {
//        Socket socket = new Socket("127.0.0.1", 9999);
//
//        PrintStream ps = new PrintStream(socket.getOutputStream());
//        Scanner scanner = new Scanner(System.in);
//        while (true) {
//            System.out.print("请输入消息：");
//            String msg = scanner.nextLine();
//            ps.println(msg);
//            ps.flush();
//        }
//    }
}
